class code4 {
    //1.两个栈：字符，数字
    public String decodeString(String s) {
        Stack<StringBuffer> st1 = new Stack<>();
        st1.push(new StringBuffer(""));
        Stack<Integer> st2 = new Stack<>();
        char[] ss = s.toCharArray();
        int i=0;
        while(i<ss.length) {
            if(ss[i]>='0' && ss[i]<='9') {
                int tmp = 0;
                while(i<ss.length &&ss[i]>='0' && ss[i]<='9') {
                    tmp = 10*tmp + (ss[i++]-'0');
                }
                st2.push(tmp);
            } else if(ss[i]=='[') {
                i++;
                StringBuffer tmp = new StringBuffer();
                while(i<ss.length &&ss[i]>='a' && ss[i]<='z') {
                    tmp.append(ss[i++]);
                }
                st1.push(tmp);
            } else if(ss[i]==']') {
                StringBuffer tmp = st1.pop();
                int k = st2.pop();
                while(k--!=0) {
                    st1.peek().append(tmp);
                }
                i++;
            } else {
                StringBuffer tmp = new StringBuffer();
                while(i<ss.length &&ss[i]>='a' && ss[i]<='z') {
                    tmp.append(ss[i++]);
                }
                st1.peek().append(tmp);
            }
        }
        return st1.peek().toString();
    }
}